/*
 *Description:
 *User:laoHu
 *Created with Intellij IDEA 2025
 *Data:2025.06.09
 *Time:14:32
 */

import Demo1.Computer;
import Demo1.KeyBoard;
import Demo1.Mouse;
import Demo2.AgeComparator;
import Demo2.NameComparator;
import Demo2.Student;
import Demo3.Person;

import  java.util.Arrays;

import java.util.Comparator;

class Value{

    int aInt;

    public Value(int aInt) {
        this.aInt = aInt;
    }
}

public class Main{

    //无法修改变量值
    public static void Swap1(int x,int y){
        int tem = x;
        x = y;
        y = tem;

    }

public static void Swap2(Value v1,Value v2){

        //创建一个临时变量，交换两个数的值
       int tem = v1.aInt;
       v1.aInt = v2.aInt;
       v2.aInt = tem;
}

    //交换两个数的值
    public static void main(String[] args) {

       // int a = 10;
       // int b = 20;
       // Swap1(a,b);

        Value value1 = new Value(10);
        Value value2 = new Value(20);

        //交换前
        System.out.print("交换前：");
        System.out.print("value1 = " + value1.aInt);
        System.out.print(" value2 = " + value2.aInt);

        Swap2(value1,value2);
        System.out.println();
        System.out.print("交换后：");
        System.out.print("value1 = " + value1.aInt);
        System.out.print(" value2 = " + value2.aInt);
    }


    //colonable接口的实现
    public static void main4(String[] args) throws CloneNotSupportedException {

        Person person1 = new Person("aa",18);
        //protected修饰的方法只能在子类中或者同一个包中使用
        //Person person2 = (Person) person1.clone();
    }

//接口comparator比较器的实现
public static void main3(String[] args) {

    Student[] students = new Student[3];
    //实例化对象
    students[0] = new Student("zhanSan",20);
    students[1] = new Student("lisi",10);
    students[2] = new Student("wangwu",40);

    AgeComparator ageComparator = new AgeComparator();

    //使用比较器:比较器的使用可以根据需求灵活的使用比较方法
    Arrays.sort(students, ageComparator);
    System.out.println("按照年龄排序后：" + Arrays.toString(students));

    NameComparator nameComparator = new NameComparator();
    Arrays.sort(students, nameComparator);
    System.out.println("按照名字排序后：" + Arrays.toString(students));
}

//冒泡排序
    public static void BubbleSort(Comparable[] comparables){

        for (int i = 0; i < comparables.length - 1; i++) {

            for (int j = 0; j < comparables.length -1 -i; j++) {

                if(comparables[j].compareTo(comparables[j+1]) > 0){

                   Comparable tem = comparables[j];
                   comparables[j] = comparables[j+1];
                   comparables[j + 1] = tem;

                }
            }
        }

    }

    //接口Compareable的实现
    public static void main2(String[] args) {

//        Student student = new Student("zhanSan",20);
//        Student student1 = new Student("zhanSan",20);

        //不能直接通过引用比较两个人的大小
        //System.out.println(student1 == student);//输出flase

        //使用comparable接口，重写compareTo方法,通过年龄比较
        //System.out.println(student.compareTo(student1));

        //使用comparable接口，重写compareTo方法，通过名字比较
        //System.out.println(student1.compareTo(student1));

        //使用冒泡排序
        Student[] students = new Student[2];
         students[0] = new Student("zhanSan",18);
         students[1] = new Student("aa",20);

         //BubbleSort(students);
        Arrays.sort(students);//按照年龄比较
        System.out.println("排序后：" + Arrays.toString(students));

    }

//接口的案例使用，IUsb接口
    public static void main1(String[] args) {

        //实例化对象
        Computer computer1 = new Computer();
        Computer computer2 = new Computer();

        //调用方法使用IUsb接口
        //打开电脑
        computer1.powerOn();

        System.out.println("------------------");

        //接入鼠标
        computer1.useIUsb(new Mouse());

        System.out.println("------------------");

        //接入键盘
        computer2.useIUsb(new KeyBoard());

        System.out.println("------------------");

        //关闭电脑
        computer1.powerOff();
    }
}